package com.iye.puretennis.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import com.iye.puretennis.R;
import com.iye.puretennis.custom.ListItemAdapter;
import com.iye.puretennis.custom.UserInfoFragment;
import com.iye.puretennis.domain.ListItem;
import com.iye.puretennis.domain.UrlSuffix;
import com.iye.puretennis.domain.UserToken;
import com.iye.puretennis.helper.Constant;
import com.iye.puretennis.helper.RemoteUrlCollections;
import com.iye.puretennis.helper.ServerAccessHelper;
import com.iye.puretennis.helper.Session;
import com.iye.puretennis.utils.ImageUtils;
import com.iye.puretennis.utils.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * User: jing wu
 * Date: 2014/12/10
 * Time: 9:15
 */
public class GroupParticipantsFragment extends TabFragment {

    private String groupGuid;

    private Boolean owner;

    private GroupParticipantsAdapter groupParticipantsAdapter;

    private LinkedList<ListItem> items = new LinkedList<ListItem>();

    public static final String TAG = "group_participants";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {

        super.onCreateView(inflater, container, savedInstanceState);

        Session session = Session.newSession();

        groupGuid = (String) session.get("groupGuid");

        owner = (Boolean) session.get("owner");

        String groupName = (String) session.get("groupName");

        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });

        headerText.setText(groupName);

        ListView groupParticipantsView = (ListView) mainView.findViewById(R.id.group_participants_list);


        groupParticipantsAdapter = new GroupParticipantsAdapter(inflater, R.layout.group_participants_item, items);

        groupParticipantsView.setAdapter(groupParticipantsAdapter);


        return mainView;
    }

    class GroupParticipantsAdapter extends ListItemAdapter {

        public GroupParticipantsAdapter(LayoutInflater inflater, int resource, LinkedList<ListItem> items) {
            super(inflater, resource, items);
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            view = initial(i, view);

            ImageView userPhotoView = (ImageView) view.findViewById(R.id.user_photo);

            final ListItem listItem = items.get(i);

            String avatar = listItem.getAvatar();

            if (StringUtils.hasText(avatar)) {
                userPhotoView.setImageBitmap(ImageUtils.base64ToBitmap(avatar));
            }

            view.setOnLongClickListener(new View.OnLongClickListener() {

                @Override
                public boolean onLongClick(View view) {
                    Session session = Session.newSession();
                    session.put("userGuid",listItem.getGuid());
                    goFragment(getSourceId(), UserInfoFragment.TAG, UserInfoFragment.class, null, false);

                    return true;
                }
            });

            return view;
        }
    }




    @Override
    public int getViewId() {
        return R.layout.group_participants;
    }

    @Override
    public int getSourceId() {
        return R.id.group_participants;
    }


    @Override
    public void onResume() {
        super.onResume();
        updateUI();
    }

    @Override
    public void updateUI() {

        listRender(token);

    }

    private void listRender(final UserToken token) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                json = ServerAccessHelper.queryStringForGet(RemoteUrlCollections.getUrl(UrlSuffix.GROUP_PARTICIPANTS) + "/" + groupGuid, token.getToken());
                getActivity().runOnUiThread(uiInvitationsUpdate);
            }
        }).start();
    }

    private Runnable uiInvitationsUpdate = new Runnable() {
        @Override
        public void run() {
            if (!Constant.RESPONSE_BADLY.equals(json)) {
                try {
                    items.clear();//remove old data
                    final JSONArray requestsArray = new JSONArray(json);
                    final int length = requestsArray.length();
                    for (int i = 0; i < length; i++) {
                        JSONObject requestObj = requestsArray.getJSONObject(i);
                        String guid = requestObj.getString("guid");
                        String name = requestObj.getString("name");
                        String avatar = requestObj.getString("avatar");
                        items.add(new ListItem(avatar, guid, name));
                    }
                    groupParticipantsAdapter.notifyDataSetChanged();

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
    };
}
